package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.dao.Order;
import com.qinghe.plant.model.OrderResponsitory;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;

@RestController
public class OrderController{
	
	@Autowired
	private OrderResponsitory orderResponsitory;
	
	@RequestMapping(value = "doReadOrders", method = {RequestMethod.GET})
    public Response ReadOrders(HttpServletRequest request) 
	{
		List<Order> orders=orderResponsitory.findAll();		
		String content=JSONArray.toJSONString(orders).toString();
		return new Response(CodeType.Success,content,"操作成功");
	}
	
	@RequestMapping(value = "doReadOrder", method = {RequestMethod.POST})
    public Response ReadOrder(HttpServletRequest request) 
	{
		String id=request.getParameter("id");
		Order order=FindOrderById(Integer.parseInt(id));
		return new Response(CodeType.Success,JSONObject.toJSONString(order),"操作成功");
	}
	
	@RequestMapping(value = "doAlterOrder", method = {RequestMethod.POST})
	public Response AlterOrder(HttpServletRequest request){		
		Order order=JSONObject.parseObject(request.getParameter("order"),Order.class);
		order= orderResponsitory.save(order);
		return new Response(CodeType.Success,JSONObject.toJSONString(order),"操作成功");
	}

	@RequestMapping(value = "doInsertOrder", method = {RequestMethod.POST})
	public Response InsertOrder(HttpServletRequest request)
	{		
		Order order=new Order();
		order.setOid(request.getParameter("Id"));
		order.setName(request.getParameter("MaterialName"));
		order.setCodification(request.getParameter("MaterialCodification"));
		order.setVersion(request.getParameter("MaterialVersion"));
		order.setBatch(request.getParameter("BatchCode"));
		order.setCount(Double.parseDouble(request.getParameter("PutInQty")));
		//order.setSdate(Date.parse(request.getParameter("PlanStartDate")) );
		//order.setFdate(Date.parse(request.getParameter("PlanFinishDate"));
		order.setState(request.getParameter("Status"));
		order.setTimer(new Date());
		order= orderResponsitory.save(order);
		return new Response(CodeType.Success,JSONObject.toJSONString(order),"操作成功");	
	}
	
	@RequestMapping(value = "doDeleteOrder", method = {RequestMethod.POST})
	public Response DeleteOrder(HttpServletRequest request)
	{
		Order order=JSONObject.parseObject(request.getParameter("order"),Order.class);
		orderResponsitory.deleteById(order.getId());	
		return new Response(CodeType.Success,null,"操作成功");
	}	
	
	public void InsetData()
	{
		System.out.println("插入一次Production数据");
	}
	
	public void DeleteData(Date date)
	{
		System.out.println("删除一次Production数据");
	}
	
	private Order FindOrderById(Integer id) {
		@SuppressWarnings("serial")
		List<Order> result = orderResponsitory.findAll(new Specification<Order>() {
			@Override
			public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("id").as(Integer.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
				}
		});
		return (result.size()!=0)?result.get(0):null;
	}		
}
